<?php
class Model_user extends Model {
    function Model_user() {
        parent::Model();
    }

    /**
     * Functie care aduce datele in grid
     */
    function getData($aPost) {

        $iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ?  $aPost["start"] : 0;
        $iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ?  $aPost["limit"] : 100;
        $sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ?  $aPost["sort"] : "name";
        $sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ?  $aPost["dir"] : "ASC";


        if (isset($aPost["activ"]) && ($aPost["activ"]==1))   {
            $this->db->where("available",$aPost["activ"]) ;
        }

        $this->db->order_by($sSort, $sDir);
        $this->db->limit($iLimit, $iStart);
        $query = $this->db->get("user");

        $totalCount = $this->db->count_all_results("user");
        $aData = $query->result_array();

        $data = array('totalCount' => $totalCount, 'data' => $aData);
        return $data;
    }


    /**
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operation($aData) {

        $aTemp = array(
            "name" => $aData["name"],
            "username" => $aData["username"],
            "email" => $aData["email"],
            "available" => $aData["available"],
            "level" => $aData["level"]
        );

        //verifica duplicatele ( vezi dbase in dir library)
        $bDuplicate = $this->dbase->verifyDuplicate("user", $aTemp, array("username"), array("user_id" => $aData["user_id"]));
        if ($bDuplicate === TRUE) {
            return "Duplicat";
        }

        //insert
        if ($aData["user_id"] == 0) {
            //daca parola nu a fost introdusa atunci parola va fi username-ul
            $aTemp["password"] = ($aData["password"] != "") ? md5($aData["password"]) : md5($aData["username"]);
            $this->db->insert("user", $aTemp);
        }
        else {

            if ($aData["password"] != "") {
                $aTemp["password"] = md5($aData["password"]);
            }

            $this->db->where("user_id", $aData["user_id"]);
            $this->db->update("user", $aTemp);
        }

        return TRUE;
    }


    /**
     * Functie care aduce datele despre o inregistrare
     */
    function getRecord($aData) {
        $this->db->select("name, username,user_id, email, available, template_style, level, level as level_val");
        $this->db->where("user_id", $aData["iRec"]);
        return $this->db->get("user")->row_array();
    }

    /**
     * Functie care schimba parola unui user
     */
    function addPass($aData) {
        $aUser = $this->usersession->currentUser();
        $this->db->where("user_id", $aUser['user_id']);
        $this->db->update("user", array("password" => md5($aData['pass'])));
        return true;
    }

    /**
     * Functie care seteaza theme-ul unui user
     */
    function setStyle($sStyle) {
        $aUser = $this->usersession->currentUser();
        $this->db->where('user_id', $aUser["user_id"]);
        $this->db->set("template_style", $sStyle);
        $this->db->update('user');
    }

    /**
     * Functie care aduce userii activii pentru un combo Ext (  to replace )
     */
    function getUserRaport() {
        $this->db->where("available", "1");
        $this->db->where("user_id !=", "1");
        $result = $this->db->get("user")->result_array();
        $temp = array();
        foreach ($result as $val) {
            $temp[$val["user_id"]] = $val["name"];
        }
        return $temp;
    }
}

?>